using System;
using System.Collections.Generic;

namespace AssemblyCSharp
{
	public class FSMRunner
	{
		private Dictionary<int, IFSMState> _StatesList;
		
		private int _CurrentState;
		
		private int _PreviousState;
		
		private int _DefferedCurrentState;
		
		public bool _EnteredState;
		
		public FSMRunner ()
		{
			_StatesList = new Dictionary<int, IFSMState>(); 
			_CurrentState = 0;
			_PreviousState = 0;
			_DefferedCurrentState = 0; 
			_EnteredState = false;
		}
		
		public void AddState(int State, IFSMState NewState)
		{
			_StatesList.Add(State, NewState);
		}
		
		public void SetCurrentState(int NewState)
		{
			_DefferedCurrentState = NewState;
		}
		
		public void SetImmediateCurrentState(int NewState)
		{
			_DefferedCurrentState = NewState;
			_CurrentState = NewState;
		}
		
		public int GetCurrentState()
		{
			return _CurrentState;
		}
		
		public int GetDefferedState()
		{
			return _DefferedCurrentState;
		}
		
		public void Update()
		{
			//update deffered state
			if(_DefferedCurrentState != _CurrentState)
			{
				_StatesList[_CurrentState].OnExit();
				_PreviousState = _CurrentState;
				_CurrentState = _DefferedCurrentState;
				_EnteredState = false;
				return;
			}
			
			if(!_EnteredState)
			{
				_StatesList[_CurrentState].OnEnter();
				_EnteredState = true;
				return;
			}
			
			_StatesList[_CurrentState].OnExecute();
		}
	}
}

